// 协助收款mock数据
let assistReceivableId = 10; // 用于生成新ID

const mockAssistReceivables = [
  {
    id: 1,
    customerCode: 'KH201',
    serviceProject: '代理记账+报税',
    customerName: '北京科技咨询公司',
    customerRating: 'A+',
    contactPerson: '张建国',
    contactPhone: '13800138201',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 16000,
    memberEnterprise: '是',
    area: '北京',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 8000,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-25 10:30:00',
    receiptNumber: 'SP2024201',
    assistant: '赵六',
    paymentRemark: '客户资金周转困难，已协调分期方案，第一期款项已到账',
    assistReason: '客户资金周转困难',
    assistLevel: '高',
    assistStatus: '进行中',
  },
  {
    id: 2,
    customerCode: 'KH202',
    serviceProject: '财务咨询',
    customerName: '上海贸易咨询',
    customerRating: 'A',
    contactPerson: '李明国',
    contactPhone: '13900139202',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 10000,
    memberEnterprise: '否',
    area: '上海',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '未关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-24 14:20:00',
    receiptNumber: '',
    assistant: '刘九',
    paymentRemark: '合同条款有争议，已安排法务部门介入协调处理',
    assistReason: '客户负责人出差',
    assistLevel: '中',
    assistStatus: '待处理',
  },
  {
    id: 3,
    customerCode: 'KH203',
    serviceProject: '税务筹划',
    customerName: '深圳创新咨询',
    customerRating: 'B+',
    contactPerson: '王华国',
    contactPhone: '13700137203',
    paymentMonth: '2024-04',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 20000,
    memberEnterprise: '是',
    area: '深圳',
    salesperson: '张十',
    accountant: '李十一',
    followWechat: '已关注',
    receivedAmount: 10000,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-23 09:15:00',
    receiptNumber: 'SP2024202',
    assistant: '陈十二',
    paymentRemark: '客户对服务质量不满意，已安排客服部门处理投诉',
    assistReason: '客户对服务有疑问',
    assistLevel: '高',
    assistStatus: '进行中',
  },
  {
    id: 4,
    customerCode: 'KH204',
    serviceProject: '审计服务',
    customerName: '广州制造咨询',
    customerRating: 'A',
    contactPerson: '陈丽国',
    contactPhone: '13600136204',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 30000,
    memberEnterprise: '是',
    area: '广州',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 20000,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-26 16:45:00',
    receiptNumber: 'SP2024203',
    assistant: '赵六',
    paymentRemark: '银行账户变更导致付款失败，已提供新的收款账户信息',
    assistReason: '客户要求分期付款',
    assistLevel: '中',
    assistStatus: '已解决',
  },
  {
    id: 5,
    customerCode: 'KH205',
    serviceProject: '代理记账',
    customerName: '杭州电商咨询',
    customerRating: 'B',
    contactPerson: '刘强国',
    contactPhone: '13500135205',
    paymentMonth: '2024-04',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 7000,
    memberEnterprise: '否',
    area: '杭州',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '未关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-22 11:20:00',
    receiptNumber: '',
    assistant: '刘九',
    paymentRemark: '客户要求延期付款，已上报主管审批，等待最终决定',
    assistReason: '客户联系方式变更',
    assistLevel: '低',
    assistStatus: '待处理',
  },
  {
    id: 6,
    customerCode: 'KH206',
    serviceProject: '财务培训',
    customerName: '北京教育咨询',
    customerRating: 'C+',
    contactPerson: '张敏国',
    contactPhone: '13400134206',
    paymentMonth: '2024-05',
    servicePeriod: '2024年3月-2024年8月',
    receivableAmount: 12000,
    memberEnterprise: '否',
    area: '北京',
    salesperson: '张十',
    accountant: '李十一',
    followWechat: '未关注',
    receivedAmount: 6000,
    receivedUpTo: '2024-03',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-21 08:30:00',
    receiptNumber: 'SP2024204',
    assistant: '陈十二',
    paymentRemark: '发票开具有误，已重新开具并寄送，客户确认收到后付款',
    assistReason: '客户对培训效果不满意',
    assistLevel: '高',
    assistStatus: '进行中',
  },
  {
    id: 7,
    customerCode: 'KH207',
    serviceProject: '工商年检',
    customerName: '成都软件咨询',
    customerRating: 'A-',
    contactPerson: '王磊国',
    contactPhone: '13300133207',
    paymentMonth: '2024-04',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 13000,
    memberEnterprise: '是',
    area: '成都',
    salesperson: '李四',
    accountant: '王五',
    followWechat: '已关注',
    receivedAmount: 6500,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-27 15:20:00',
    receiptNumber: 'SP2024205',
    assistant: '赵六',
    paymentRemark: '客户财务流程较长，已提供全套收款资料，等待内部审批',
    assistReason: '客户要求延期付款',
    assistLevel: '中',
    assistStatus: '已解决',
  },
  {
    id: 8,
    customerCode: 'KH208',
    serviceProject: '税务申报',
    customerName: '武汉物流咨询',
    customerRating: 'B+',
    contactPerson: '陈刚国',
    contactPhone: '13200132208',
    paymentMonth: '2024-03',
    servicePeriod: '2024年1月-2024年6月',
    receivableAmount: 8500,
    memberEnterprise: '否',
    area: '武汉',
    salesperson: '王七',
    accountant: '陈八',
    followWechat: '已关注',
    receivedAmount: 0,
    receivedUpTo: '',
    paymentStatus: '未收款',
    lastPushTime: '2024-03-28 09:45:00',
    receiptNumber: '',
    assistant: '刘九',
    paymentRemark: '客户系统升级影响付款，预计下周完成，已做好跟进准备',
    assistReason: '客户财务人员离职',
    assistLevel: '低',
    assistStatus: '待处理',
  },
  {
    id: 9,
    customerCode: 'KH209',
    serviceProject: '财务咨询',
    customerName: '南京科技咨询',
    customerRating: 'A',
    contactPerson: '李建国',
    contactPhone: '13100131209',
    paymentMonth: '2024-04',
    servicePeriod: '2024年1月-2024年12月',
    receivableAmount: 18000,
    memberEnterprise: '是',
    area: '南京',
    salesperson: '张十',
    accountant: '李十一',
    followWechat: '已关注',
    receivedAmount: 9000,
    receivedUpTo: '2024-02',
    paymentStatus: '部分收款',
    lastPushTime: '2024-03-29 14:15:00',
    receiptNumber: 'SP2024206',
    assistant: '陈十二',
    paymentRemark: '节假日期间暂停收款，已与客户约定节后第一时间处理',
    assistReason: '客户对发票有疑问',
    assistLevel: '中',
    assistStatus: '进行中',
  },
];

export default {
  // 获取协助收款列表
  'GET /api/assist-receivables': (req, res) => {
    const { status, current = 1, pageSize = 10, ...params } = req.query;
    
    // 根据状态过滤
    let filteredData = mockAssistReceivables;
    if (status === '未收款') {
      filteredData = mockAssistReceivables.filter(item => item.paymentStatus === '未收款');
    } else if (status === '部分收款') {
      filteredData = mockAssistReceivables.filter(item => item.paymentStatus === '部分收款');
    } else if (status === '已收齐') {
      filteredData = mockAssistReceivables.filter(item => item.paymentStatus === '已收齐');
    }
    
    // 根据协助状态过滤
    if (params.assistStatus) {
      filteredData = filteredData.filter(item => item.assistStatus === params.assistStatus);
    }
    
    // 搜索过滤
    if (params.customerName) {
      filteredData = filteredData.filter(item => 
        item.customerName.includes(params.customerName)
      );
    }
    if (params.customerCode) {
      filteredData = filteredData.filter(item => 
        item.customerCode.includes(params.customerCode)
      );
    }
    if (params.contactPerson) {
      filteredData = filteredData.filter(item => 
        item.contactPerson.includes(params.contactPerson)
      );
    }
    
    // 分页
    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + parseInt(pageSize);
    const pagedData = filteredData.slice(startIndex, endIndex);
    
    res.json({
      success: true,
      data: {
        list: pagedData,
        total: filteredData.length,
        current: parseInt(current),
        pageSize: parseInt(pageSize),
      },
    });
  },

  // 创建协助收款记录
  'POST /api/assist-receivables': (req, res) => {
    const newRecord = {
      id: assistReceivableId++,
      ...req.body,
      paymentStatus: '未收款',
      receivedAmount: 0,
      receivedUpTo: '',
      receiptNumber: '',
      assistStatus: '待处理',
    };
    mockAssistReceivables.unshift(newRecord);
    
    res.json({
      success: true,
      data: newRecord,
      message: '协助收款记录创建成功',
    });
  },

  // 更新协助收款记录
  'PUT /api/assist-receivables/:id': (req, res) => {
    const { id } = req.params;
    const index = mockAssistReceivables.findIndex(item => item.id == id);
    
    if (index !== -1) {
      mockAssistReceivables[index] = { ...mockAssistReceivables[index], ...req.body };
      res.json({
        success: true,
        data: mockAssistReceivables[index],
        message: '协助收款记录更新成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '协助收款记录不存在',
      });
    }
  },

  // 删除协助收款记录
  'DELETE /api/assist-receivables/:id': (req, res) => {
    const { id } = req.params;
    const index = mockAssistReceivables.findIndex(item => item.id == id);
    
    if (index !== -1) {
      mockAssistReceivables.splice(index, 1);
      res.json({
        success: true,
        message: '协助收款记录删除成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '协助收款记录不存在',
      });
    }
  },
}; 